import type { FiveElement } from '../../constants/five-elements';
import type { ItemId } from '../../data/item';
import type { CultivationSkillItemId } from '../../data/item/cultivation-skill';
import { SECT_JOB_DATA } from '../../data/sect/type';
import { ItemReader } from '../item/reader';
import {
  CONDITION_TYPE,
  type AndCondition,
  type Condition,
  type CultivationSkillStudyCountCondition,
  type ItemCondition,
  type SectJobCondition,
  type NotCondition,
  type OrCondition,
  type RealmCondition,
  type SpiritualRootCondition,
  type BattleLevelCondition,
  type SpiritStoneCondition,
  type LifeCondition,
  type CultivationSkillCondition,
} from './type';

export class ConditionFactory {
  /** 境界条件 */
  static createRealmCondition(
    realm: number,
    description?: string
  ): RealmCondition {
    return {
      type: CONDITION_TYPE.REALM,
      realm,
      description: description || `境界达到 ${realm} 重`,
    };
  }

  /** 功法学习数量条件 */
  static createCultivationSkillStudyCountCondition(
    count: number,
    description?: string
  ): CultivationSkillStudyCountCondition {
    return {
      type: CONDITION_TYPE.CULTIVATION_SKILL_STUDY_COUNT,
      count,
      description: description || `学习 ${count} 种功法`,
    };
  }

  /** 物品条件 */
  static createItemCondition(
    itemId: ItemId,
    count: number,
    description?: string
  ): ItemCondition {
    const itemConfig = ItemReader.getConfig(itemId);

    return {
      type: CONDITION_TYPE.ITEM,
      itemId,
      count,
      description:
        description || `拥有 [${itemConfig?.name || '未知物品'}] ${count} 个`,
    };
  }

  /** 宗门职位条件 */
  static createSectJobCondition(
    job: number,
    description?: string
  ): SectJobCondition {
    const sectJobConfig = SECT_JOB_DATA.find((item) => item.level === job);
    const jobName = sectJobConfig ? sectJobConfig.name : '未知职位';
    return {
      type: CONDITION_TYPE.SECT_JOB,
      job,
      description: description || `宗门职位达到 [${jobName}]`,
    };
  }

  /** 拥有灵根条件 */
  static createSpiritualRootCondition(
    spiritualRoot: FiveElement[],
    description?: string
  ): SpiritualRootCondition {
    return {
      type: CONDITION_TYPE.SPIRITUAL_ROOT,
      spiritualRoot,
      description: description || `拥有灵根`,
    };
  }

  /** 战斗等级条件 */
  static createBattleLevelCondition(
    level: number,
    description?: string
  ): BattleLevelCondition {
    return {
      type: CONDITION_TYPE.BATTLE_LEVEL,
      level,
      description: description || `战斗等级达到 ${level} 级`,
    };
  }

  /** 灵石数量条件 */
  static createStoneCondition(
    stone: number,
    description?: string
  ): SpiritStoneCondition {
    return {
      type: CONDITION_TYPE.SPIRIT_STONE,
      count: stone,
      description: description || `需要 ${stone} 个灵石`,
    };
  }

  /** 寿元条件 */
  static createLifeCondition(
    years: number,
    description?: string
  ): LifeCondition {
    return {
      type: CONDITION_TYPE.LIFE,
      years,
      description: description || `修炼 ${years} 年`,
    };
  }

  /** 前置功法条件 */
  static createCultivationSkillCondition(
    id: CultivationSkillItemId,
    description?: string
  ): CultivationSkillCondition {
    return {
      type: CONDITION_TYPE.CULTIVATION_SKILL,
      id,
      description: description || `学习 ${id}`,
    };
  }

  /** AND 复合条件 */
  static createAndCondition(
    conditions: Condition[],
    description?: string
  ): AndCondition {
    return {
      type: CONDITION_TYPE.AND,
      conditions,
      description: description || `满足所有条件`,
    };
  }

  /** OR 复合条件 */
  static createOrCondition(
    conditions: Condition[],
    description?: string
  ): OrCondition {
    return {
      type: CONDITION_TYPE.OR,
      conditions,
      description: description || `满足任一条件`,
    };
  }

  /** NOT 条件 */
  static createNotCondition(
    condition: Condition,
    description?: string
  ): NotCondition {
    return {
      type: CONDITION_TYPE.NOT,
      condition,
      description: description || `不满足条件`,
    };
  }
}
